package com.example.wac.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.wac.annotation.UnJwtToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author wuancheng
 * @description
 * @date 2023-03-21 16:58:00
 */

@Slf4j
@RestController
@RequestMapping("/sentinel")
public class SentinelController {

    @GetMapping("/hello")
    @SentinelResource(value = "hello", blockHandler = "blockHandler", fallback = "defaultFallback")
    @UnJwtToken
    public String hello(@RequestParam("name") String name){
        if (Objects.equals(name, "oh")) {
            throw new RuntimeException("oh");
        }
        return "hello " + name;

    }

    public String blockHandler(BlockException blockExe){
        return "网络拥堵，请稍后再试";
    }

    public String defaultFallback(String userId) {
        return "defaultFallback降级了";
    }
}
